package cn.stylefeng.guns.modular.cb.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbCustomerStudents;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerStudentsParam;
import cn.stylefeng.guns.modular.cb.model.params.CbMessagesParam;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerStudentsResult;
import cn.stylefeng.guns.modular.cb.service.CbCustomerStudentsService;
import cn.stylefeng.guns.modular.cb.service.CbMessagesService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author wj
 * @Date 2021-01-07 22:44:39
 */
@Controller
@RequestMapping("/api/customer_students")
public class CbCustomerStudentsController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(CbCustomerStudentsController.class);

    private String PREFIX = "/cb/cbCustomerStudents";

    @Autowired
    private CbCustomerStudentsService cbCustomerStudentsService;
    @Autowired
    private CbMessagesService cbMessagesService;

    @Autowired
    UserService userService;

    /**
     * 跳转到主页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/cbCustomerStudents.html";
    }

    /**
     * 新增页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/cbCustomerStudents_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wj
     * @Date 2021-03-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/cbCustomerStudents_edit.html";
    }

    @RequestMapping("/detail_p")
    public String detail() {
        return PREFIX + "/cbCustomerStudents_detail.html";
    }

    /**
     * 新增接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(CbCustomerStudentsParam cbCustomerStudentsParam) {
        try{
            this.cbCustomerStudentsService.add(cbCustomerStudentsParam);

            //添加成功后给学生发送注册成功的消息
            CbMessagesParam messagesParam = new CbMessagesParam();
            messagesParam.setType(9L);
            messagesParam.setMessageUserid(cbCustomerStudentsParam.getId());
            messagesParam.setMessage("恭喜注册成为会员，如果在使用上有什么问题可以进行反馈。");
            messagesParam.setStatus(0L);
            cbMessagesService.add(messagesParam);
        }catch (Exception e) {
            logger.error("student add error：", e);
        }
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(CbCustomerStudentsParam cbCustomerStudentsParam) {
        this.cbCustomerStudentsService.update(cbCustomerStudentsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(CbCustomerStudentsParam cbCustomerStudentsParam) {
        this.cbCustomerStudentsService.delete(cbCustomerStudentsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(CbCustomerStudentsParam cbCustomerStudentsParam) {
        CbCustomerStudents detail = this.cbCustomerStudentsService.getById(cbCustomerStudentsParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayuiPageInfo list(CbCustomerStudentsParam cbCustomerStudentsParam) {
        return this.cbCustomerStudentsService.findPageBySpec(cbCustomerStudentsParam);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-07
     */
    @RequestMapping("/student_list")
    @ResponseBody
    public LayuiPageInfo studentList(CbCustomerStudentsParam cbCustomerStudentsParam) {
        return this.cbCustomerStudentsService.studentList(cbCustomerStudentsParam);
    }

    /**
     * 获取学生
     * @return
     */
    @RequestMapping("/get_student_user")
    @ResponseBody
    public ResponseData getStudentByUser(){
        Long userId = LoginContextHolder.getContext().getUserId();
        CbCustomerStudentsParam param = new CbCustomerStudentsParam();
        param.setCreateUser(userId);
        CbCustomerStudentsResult detail = this.cbCustomerStudentsService.getStudent(param);
        return ResponseData.success(detail);
    }

    /**
     * 学长的校友
     * @param cbCustomerStudentsParam
     * @return
     */
    @RequestMapping("/alumni_student_list")
    @ResponseBody
    public LayuiPageInfo alumniByStaffList(CbCustomerStudentsParam cbCustomerStudentsParam) {
        LayuiPageInfo list = this.cbCustomerStudentsService.alumniByStudentList(cbCustomerStudentsParam);
        return list;
    }

    /**
     * 年级
     * @return
     */
    @RequestMapping("/list_grade")
    @ResponseBody
    public ResponseData getGrade(){
        LocalDate date = LocalDate.now();
        Integer year = date.getYear();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add(year-i);
        }
        return ResponseData.success(list);
    }
}


